package float

import (
	"fmt"
	"math"
	"strconv"
)

func FormatFloat(value float64, precision int) (res float64) {
	n10 := math.Pow10(precision)
	return math.Trunc((value+0.5/n10)*n10) / n10
}

func CompareFloat(bigValue float64, smallValue float64) (res int) {
	n10 := math.Pow10(10)
	zoomBigValue := math.Trunc((bigValue + 0.5/n10) * n10)
	zoomSmallValue := math.Trunc((smallValue + 0.5/n10) * n10)
	if zoomBigValue > zoomSmallValue {
		return 1
	} else if zoomBigValue < zoomSmallValue {
		return -1
	}
	return 0
}

func SPrintfFloat(value float64, precision int) (res string) {
	return fmt.Sprintf("%."+strconv.Itoa(precision)+"f", FormatFloat(value, precision))
}
